package sort;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yi
 * Date: 2024-07-28
 * Time: 21:00
 */

/**
 * 选择排序
 */
public class SelectSort {

    private static void swap(int[] array,int i,int j){
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
    public static void sort(int[] array){
        for (int i = 0; i < array.length - 1; i++) {
            int minIndex = i;
            for (int j = i+1; j < array.length; j++) {
                if(array[j] < array[minIndex]){
                    minIndex = j;
                }
            }
            int tmp = array[i];
            array[i] = array[minIndex];
            array[minIndex] = tmp;
        }
    }

    //优化方案
 /*   public static void sort(int[] array){
        int left = 0;
        int right = array.length - 1;
        while(left < right){
            int minValue = left;
            int maxValue = left;
            for (int i = left+1; i <= right; i++) {
                if(array[i] < array[minValue]){
                    minValue = i;
                }
                if(array[i] > array[maxValue]){
                    maxValue = i;
                }
            }
            swap(array,left,minValue);
            if(maxValue == left){
                maxValue = minValue;
            }
            swap(array,right,maxValue);
            left++;
            right--;
        }
    }*/
}
